package com.tansun.easycare.rule.datacomponent.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.datacomponent.entity.RuleDatacomponent;
import com.tansun.easycare.rule.datacomponent.model.DataComponentEntity;
import com.tansun.easycare.rule.datacomponent.service.IDataComponentService;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.utils.JsonUtil;
import com.tansun.rule.common.RuleModelTypeEnum;

/**
 * 自定义函数
 *
 * @author WYH 2017年9月11日
 */
@Controller
@RequestMapping(value = "/ruleProj/dataComponent")
public class DataComponentController extends RuleBaseController {
    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private IDataComponentService dataComponentService;

    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response, RuleManageVO param, ModelMap map) throws Exception {
        param.setProjectCode(getCurrProjectCode());
        param.setModelType(RuleModelTypeEnum.DataComponet.getCode());
        PageInfo<RuleManageVO> page = this.ruleManageService.findRuleManageVO(new Page<RuleManageVO>(), param);
        map.put("projectCode", this.getCurrProjectCode());
        map.put("page", page);
        map.put("vo", JsonUtil.object2json(param));
        return "rule/datacomponent/index";
    }

    /**
     * 跳转至新增页面
     */
    @RequestMapping("detail")
    public String detail(String dcId, String isedit, String version, ModelMap map) throws Exception {

        if (!StringUtils.isEmpty(dcId)) {
            RuleManage dcModel = this.ruleManageService.getById(dcId);
            map.put("dcModel", dcModel);
            if (StringUtils.isEmpty(version)) {
                version = dcModel.getVersion();
            }
        }
        map.put("version", version);
        map.put("projectCode", this.getCurrProjectCode());
        map.put("isedit", isedit);
        return "rule/datacomponent/datacomponentDetail";
    }

    @RequestMapping(value = "getIndexDetail")
    public @ResponseBody
    JsonMessage getIndexDetail(String dcId, String version) {
        JsonMessage ret = new JsonMessage();
        try {
            if (StringUtils.isEmpty(dcId))
                throw new BizException("E-100001");
            RuleDatacomponent condition = new RuleDatacomponent();
            condition.setManageId(dcId);
            condition.setVersion(version);
            List<RuleDatacomponent> results = dataComponentService.findList(condition);
            ret.setData(results.get(0));
        } catch (Exception e) {
            ret.setMessage(e.getMessage());
            ret.setResult(false);
        }
        return ret;
    }

    @RequestMapping("saveOrUpdate")
    public @ResponseBody
    JsonMessage saveOrUpdate(@RequestBody DataComponentEntity entity) {
        JsonMessage ret = new JsonMessage();
        try {
            dataComponentService.saveOrUpdate(entity);
        } catch (Exception e) {
            ret.setResult(false);
            ret.setMessage(e.getMessage());
        }
        return ret;
    }

    @Override
    public void setRuleBizService() {
        // TODO Auto-generated method stub
        super.setRuleBaseService(dataComponentService);
    }


}
